package net.cardgame.orcalecard;

import java.util.ArrayList;
import java.util.HashMap;

import jp.jma.oraclecard.R;
import net.cardgame.orcalecard.utils.ConstantValue;
import net.cardgame.orcalecard.utils.NetworkUtils;
import net.cardgame.orcalecard.utils.XmlParserInfoApp;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class InfoAppActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	// XML node keys
	private static final String KEY_ENTRY = "entry";
	private static final String KEY_TITLE = "title";
	private static final String KEY_LINK = "link";
	private static final String KEY_SUMMARY = "summary";
	private static final String KEY_POSTED = "posted";

	private ArrayList<HashMap<String, String>> entrysList;
	private XmlParserInfoApp parser;
	private String xml = "";
	private String url = ConstantValue.URL_XML_INFOAPP;
	private ListView listView;
	private Button btnBack;

	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.info_app_layout);

		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		if (NetworkUtils.isNetworkConnected(InfoAppActivity.this))
			new LoadDataAsyncTask().execute();
		else {
			showAlertDialog(InfoAppActivity.this, "No Internet Connection",
					"You don't have internet connection.");
		}
	}

	private class LoadDataAsyncTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(InfoAppActivity.this, "",
					"Loading..Wait..", true);
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			processData();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			dialog.dismiss();
			listView = (ListView) findViewById(R.id.listView);
			AdapterInfoApp adapter = new AdapterInfoApp(InfoAppActivity.this,
					entrysList);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(InfoAppActivity.this);
			super.onPostExecute(result);
		}
	}

	public void processData() {
		entrysList = new ArrayList<HashMap<String, String>>();
		parser = new XmlParserInfoApp();
		xml = parser.getXmlFromUrl(url);

		if (xml != null) {
			xml = xml.substring(xml.indexOf("<entry>"), xml.length());

			// Chen tag "<root>" va "</root>" vao dau va cuoi xml
			xml = "<root>" + xml + "</root>";

			Document doc = parser.getDomElement(xml);
			NodeList nl = doc.getElementsByTagName(KEY_ENTRY);

			// looping through all song nodes <song>
			for (int i = 0; i < nl.getLength(); i++) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				Element e = (Element) nl.item(i);
				// adding each child node to HashMap key => value
				map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
				map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
				map.put(KEY_SUMMARY, parser.getValue(e, KEY_SUMMARY));
				map.put(KEY_POSTED, parser.getValue(e, KEY_POSTED));

				// adding HashList to ArrayList
				entrysList.add(map);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		String url = entrysList.get(position).get(KEY_LINK);
		if (url != null && url != "") {
			Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
			startActivity(myIntent);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		default:
			break;
		}
	}

	// Show dialog alert no internet connection
	public void showAlertDialog(Context context, String title, String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		// Setting alert dialog icon
		alertDialog.setIcon(R.drawable.fail_connect_internet);

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}
}
